package org.inspiration.dudu.web.service.content.imp;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.util.FileUtil;
import org.inspiration.dudu.content.model.dto.ArticleDTO;
import org.inspiration.dudu.content.model.entity.ArticleEntity;
import org.inspiration.dudu.content.model.vo.ArticleSimpleVO;
import org.inspiration.dudu.content.model.vo.ArticleVO;
import org.inspiration.dudu.content.service.ArticleService;
import org.inspiration.dudu.user.service.UserFavoriteService;
import org.inspiration.dudu.user.service.UserService;
import org.inspiration.dudu.web.service.content.ArticleFacadeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

/**
 * 文章外观服务实现类
 * 提供文章的上传、查找和获取最近文章的功能
 */
@Service
@RequiredArgsConstructor
public class ArticleFacadeServiceImpl implements ArticleFacadeService {

    @Value("${dudu.file.upload-path}")
    private String uploadPath; // 文件上传路径

    private final FileUtil fileUtil; // 文件工具类
    private final ArticleService articleService; // 文章服务
    private final UserFavoriteService userFavoriteService; // 用户收藏服务
    private final UserService userService; // 用户服务

    /**
     * 上传文章
     *
     * @param articleDTO 文章数据传输对象
     * @return 文章的唯一标识符
     */
    @Override
    @Transactional
    public String uploadArticle(ArticleDTO articleDTO) {
        String uuid = UUID.randomUUID().toString(); // 生成唯一标识符
        String filename = uuid + ".html"; // 生成文件名
        Path subPath = Paths.get("articles", filename); // 文章文件的相对路径
        // 将内容存入文件
        fileUtil.upload(articleDTO.getContent(), subPath);
        // 将文章实体存入数据库
        String contentFilePath = Paths.get(uploadPath).resolve(subPath).toString();
        ArticleEntity articleEntity = articleService.transToEntity(articleDTO, contentFilePath, uuid);
        articleService.createArticle(articleEntity);
        // 增加发布者的发布文章数量
        userService.increaseArticleCount(articleDTO.getUserId());
        return uuid; // 返回文章的唯一标识符
    }
    @Override
    @Transactional
    public void updateArticle(ArticleDTO articleDTO, String id) {
        String filename = id + ".html";
        Path subPath = Paths.get("articles", filename);
        String contentFilePath = Paths.get(uploadPath).resolve(subPath).toString();
        fileUtil.upload(articleDTO.getContent(), subPath);
        articleService.updateArticle(articleService.transToEntity(articleDTO, contentFilePath, id));

    }

    @Override
    public void updateArticleStatus(String id, String status) {
        articleService.updateArticleStatus(id, status);
    }

    @Override
    public List<ArticleSimpleVO> getArticlesByUserIdAndKeyWordOrderByDESC(Long userId, String keyWord, Integer pageNum, Integer pageSize) {
        List<ArticleSimpleVO> list = articleService.getArticlesByUserIdAndKeyWordOrderByDESC(userId, keyWord, pageNum, pageSize);
        return list;
    }

    @Override
    public List<ArticleSimpleVO> getPublishedArticlesByKeyWordOrderByDESC(String keyWord, Integer pageNum, Integer pageSize) {
        List<ArticleSimpleVO> list = articleService.getPublishedArticlesByKeyWordOrderByDESC(keyWord, pageNum, pageSize);
        return list;
    }

    /**
     * 根据文章ID查找文章
     *
     * @param id 文章ID
     * @return 文章视图对象
     */
    @Override
    public ArticleVO getArticleById(String id) {
        return articleService.getArticleById(id);
    }

    /**
     * 获取最近的文章列表
     *
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 最近文章的简单视图对象列表
     */
    @Override
    public List<ArticleSimpleVO> getPublishedArticlesOrderByDESC(Integer pageNum, Integer pageSize) {
        List<ArticleSimpleVO> list = articleService.getPublishedArticlesOrderByDESC(pageNum, pageSize);
        return list;
    }

    @Override
    public List<ArticleSimpleVO> getPublishedArticlesByUserIdOrderByDESC(Long userId, Integer pageNum, Integer pageSize) {
        List<ArticleSimpleVO> list = articleService.getPublishedArticlesByUserIdOrderByDESC(userId, pageNum, pageSize);
        return list;
    }

    @Override
    public List<ArticleSimpleVO> getDraftArticlesByUserIdOrderByDESC(Long userId, Integer pageNum, Integer pageSize) {
        List<ArticleSimpleVO> list = articleService.getDraftArticlesByUserIdOrderByDESC(userId, pageNum, pageSize);
        return list;
    }



}
